package com.wms.controller;

import com.wms.common.Result;
import com.wms.entity.Port;
import com.wms.service.PortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/port")
public class PortController {

    @Autowired
    private PortService portService;

    // 获取所有港口信息
    @GetMapping("/list")
    public Result<List<Port>> list() {
        List<Port> ports = portService.list();
        return Result.suc("获取成功", ports);
    }

    // 根据ID获取港口信息
    @GetMapping("/{id}")
    public Result<Port> getById(@PathVariable Integer id) {
        Port port = portService.getById(id);
        if (port != null) {
            return Result.suc("获取成功", port);
        } else {
            return Result.fail("港口不存在");
        }
    }

    // 新增港口
    @PostMapping("/add")
    public Result<?> add(@RequestBody Port port) {
        boolean saveSuccess = portService.save(port);
        if (saveSuccess) {
            return Result.suc("新增成功");
        } else {
            return Result.fail("新增失败");
        }
    }

    // 更新港口信息
    @PutMapping("/update")
    public Result<?> update(@RequestBody Port port) {
        boolean updateSuccess = portService.updateById(port);
        if (updateSuccess) {
            return Result.suc("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    // 删除港口
    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        boolean removeSuccess = portService.removeById(id);
        if (removeSuccess) {
            return Result.suc("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}
